﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model;
using System.Net.Http;
using 报表导出.Model;
using 报表导出.Common;

namespace EasyUIDemo.MVC.Controllers
{
    public class BasicController : Controller
    {
        //
        // GET: /Basic/
        /// <summary>
        /// 信息展示
        /// </summary>
        /// <param name="where"></param>
        /// <param name="pageindex"></param>
        /// <returns></returns>
        public ActionResult basic_show(string where,int id,int pageindex=1)
        {
            int pagesize=8;
            string strwhere="";
            if(where!=null)
            {
                strwhere += " and a.projectNumber="+where+"  ";
            }
            
            using (HttpClient client = new HttpClient())
            {
                string strurl = "http://localhost:63805/apis/Basic_info/listget?pageindex="+pageindex+"&pagesize="+pagesize+"&wherestr="+strwhere+"&id="+id;
                var result = client.GetAsync(strurl).Result;
                if (result.IsSuccessStatusCode)
                {
                    var BasicModel = result.Content.ReadAsStringAsync().Result;
                    var list = Newtonsoft.Json.JsonConvert.DeserializeObject<pagelist<Basic_Info>>(BasicModel);
                    ViewBag.index = pageindex;
                    ViewBag.pagecount = list.pagecount;
                    ViewBag.count = list.count ;
                    ViewBag.id = id;
                    return  View(list.getlist);
                }
                else
                {
                    return Content("<script>alert('请求出错')</script>");
                }
            }
            
        }
        /// <summary>
        /// 加载基本信息
        /// </summary>
        /// <returns></returns>
        public ActionResult Basic_Info()
        {
            var  dropDic = Common.EnumListDic("请选择", "住宅", "0");
            ViewBag.house_Purpose = new SelectList(dropDic, "value", "text");
            var  Source = Common.EnumListDic("请选择", "来源", "0");
            ViewBag.yers_Source = new SelectList(Source, "value", "text");
            var type = Common.EnumListDic("请选择", "报告类型", "0");
            ViewBag.presentation_Type = new SelectList(type, "value", "text");
            //Dictionary<string, object> assessment = Common.EnumListDic("0", "评估受理", "请选择");
            ViewBag.assessment = ((List<Dictionary>)Session["dictionary"]).Where(n => n.dictionary_Type == "评估受理").ToList();

            //Dictionary<string, object> employ = Common.EnumListDic("请选择", "部门", "0");
            //ViewBag.the_Department = new SelectList(employ, "value", "key");
            ViewBag.the_Department = new SelectList(getdepList(), "department_Id", "department_Name");
            ViewBag.survey = new SelectList(GetUsers(2), "users_Id", "users_Name");//勘察原
            ViewBag.evaluation = new SelectList(GetUsers(3), "users_Id", "users_Name");//评估员
            
            return View();
        }
        /// <summary>
        /// 请求部门的列表用于下拉框的绑定
        /// </summary>
        /// <returns></returns>
        public List<Department> getdepList()
        {
            using(HttpClient client=new HttpClient ())
            {
                string strurl = "http://localhost:63805/apis/Department/DeptShow";
                var result = client.GetAsync(strurl).Result;
                if(result.IsSuccessStatusCode)
                {
                    var deprModel = result.Content.ReadAsStringAsync().Result;
                    var list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Department>>(deprModel);
                    return list;
                }
                else
                {
                    return null;
                }
            }
        }
        /// <summary>
        /// 根据id查找相应的人员
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Users> GetUsers(int id)
        {
            using (HttpClient client = new HttpClient())
            {
                string strurl = "http://localhost:63805/api/Users/"+id;
                var result = client.GetAsync(strurl).Result;
                if (result.IsSuccessStatusCode)
                {
                    var deprModel = result.Content.ReadAsStringAsync().Result;
                    var list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Users>>(deprModel);
                    return list;
                }
                else
                {
                    return null;
                }
            }
        }

        public ActionResult Dot(int id)
        {
            Basic_Info ba=new Model.Basic_Info ();
            string strurl = "http://localhost:63805/api/Basic_info/"+id;
            using(HttpClient client=new HttpClient ())
            {
                var result=client.GetAsync(strurl).Result;
                if(result.IsSuccessStatusCode)
                {
                    var model=result.Content.ReadAsStringAsync().Result;
                    ba=Newtonsoft.Json.JsonConvert.DeserializeObject<Basic_Info>(model);
                }
                else
                {
                    return Content("<script>alert('导出过程出错！');<script>");
                }
            }


            //模板地址
            string TemplateName = Server.MapPath("\\Template\\离职证明.doc");

            //保存地址
            string SaveName = Server.MapPath(string.Format("\\Template\\离职证明{0}.doc", DateTime.Now.ToString("yyyyMMdd")));

            //模板内容
            List<WordsTemp> lwt = new List<WordsTemp>();
            lwt.Add(new WordsTemp() { Name = "姓名", Value =ba.projectNumber  });
            lwt.Add(new WordsTemp() { Name = "入年", Value = ba.basic_CreateTime.ToString() });
            lwt.Add(new WordsTemp() { Name = "入月", Value = ba.basic_State.ToString() });
            lwt.Add(new WordsTemp() { Name = "岗位", Value = ba.basic_CreatePeople.ToString() });
            lwt.Add(new WordsTemp() { Name = "离年", Value = ba.presentation_Type.ToString() });
            lwt.Add(new WordsTemp() { Name = "离月", Value = ba.basic_id.ToString() });
            lwt.Add(new WordsTemp() { Name = "离日", Value = ba.department_Name.ToString() });
            lwt.Add(new WordsTemp() { Name = "公司", Value = ba.projectNumber });

            lwt.Add(new WordsTemp() { Name = "年", Value = DateTime.Now.Year.ToString() });
            lwt.Add(new WordsTemp() { Name = "月", Value = DateTime.Now.Month.ToString() });
            lwt.Add(new WordsTemp() { Name = "日", Value = DateTime.Now.Day.ToString() });

            CommonWords.Word(TemplateName, SaveName, lwt);

            return Content(string.Format("<script>location.href = '{0}';</script>", urlconvertor(SaveName)));
        }



        /// <summary>
        /// 本地路径转换成URL相对路径
        /// </summary>
        /// <param name="imagesurl1"></param>
        /// <returns></returns>
        private string urlconvertor(string imagesurl1)
        {
            string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录
            string imagesurl2 = imagesurl1.Replace(tmpRootDir, ""); //转换成相对路径
            imagesurl2 = imagesurl2.Replace(@"\", @"/");
            //imagesurl2 = imagesurl2.Replace(@"Aspx_Uc/", @"");
            return "/" + imagesurl2;
        }
        

    }
}
